package jm.easyconfig.standalone;

import java.net.URI;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import jm.easyconfig.EasyValue;
import jm.easyconfig.ValueExt;

@Service
public class CfgService {
	@Value("${test.port2:222}")
	public int port2;
	
	@EasyValue("${test.url.noexist}")
	public URI test_url;

	@Value("${path}")
	public String path;
	
	public int port;

	@EasyValue(value = "${test.port:8080}", name = "XXX服务端口")
	public void setPort(int port) {
		this.port = port;
	}

	public int getPort() {
		return port;
	}

	@Value("${test.appKey:}")
	public String test_appKey;

	@Value("${test.appSecurity:}")
	public String test_appSecurity;

	public static class DataBean {
		public String name;
		public int age;
	}

	@EasyValue("${test.datalist}")
	public DataBean[] arrayStyle;

	@EasyValue("${test.datalist}")
	public List<DataBean> listLimited;

	@EasyValue("${test.datalist:}")
	public String test_complex_text;

	public static class ComplexBean {
		public List<DataBean> list;
		public int anyvalue = 9;
		String anything = "hello easyconfig";
	}

	@EasyValue("${test.complex:}")
	public ComplexBean beanStyle;

	@PostConstruct
	public void init() throws Throwable {
		ObjectMapper objectMapper = new ObjectMapper();
		CfgService service = objectMapper.readValue("{\"listLimited\":" + test_complex_text + "}", CfgService.class);

		System.out.println(this.arrayStyle);

	}

	@Override
	public String toString() {
		return "CfgService [test_url=" + test_url + ", port=" + port + ", test_appKey=" + test_appKey
				+ ", test_appSecurity=" + test_appSecurity + ", test_complex=" + arrayStyle + ", t=" + t + "]";
	}

	@ValueExt(value = "${test.appKey1:a}", name = "hello annotation")
	public String t;
}
